import java.lang.reflect.*;

public class Demo2 {

    // 创建对象
    public static void reflectNewInstance() {
        Class<?> c1;
        try {
            c1 = Class.forName("Student");
            Student student = (Student) c1.newInstance();

            System.out.println("获得学生对象："+student);
         } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> c1;
        try {
            c1 = Class.forName("Student");

            c1.getDeclaredConstructor(String.class, int.class);

            Constructor<Student> constructor =
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class, int.class);
            //设置私有的在类外可以使用
            constructor.setAccessible(true);

            Student student = constructor.newInstance("zhangsan",18);
            System.out.println("获得私有的构造方法");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有属性
    public static void reflectPrivateField() {
        Class<?> c1;
        try {
            c1 = Class.forName("Student");
            Field field = c1.getDeclaredField("name");
            //设置私有的在类外可以使用
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();

            field.set(student,"lisi");
            System.out.println("获得私有的属性并修改 name");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void reflectPrivateMethod() {
        Class<?> c1;
        try {
            c1 = Class.forName("Student");
            Method method = c1.getDeclaredMethod("function",String.class);
            Student student = (Student) c1.newInstance();

            method.setAccessible(true);
            method.invoke(student,"我是一个参数");
            System.out.println("获得私有的方法");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {

        reflectNewInstance();

    }
}
